//
//  TTGradientAnimationLayer.m
//  ITBE
//
//  Created by ITBE on 2019/11/15.
//  Copyright © 2019 ITBE. All rights reserved.
//

#import "TTGradientAnimationLayer.h"
@interface TTGradientAnimationLayer(){
    NSMutableArray *_layers;
    NSMutableArray *_animations;
}
@property(nonatomic, assign)CGFloat radius;
@end

@implementation TTGradientAnimationLayer


+ (instancetype)showWithFrame:(CGRect)frame cornerRadius:(CGFloat)radius inView:(UIView *)view{
    TTGradientAnimationLayer *layer = TTGradientAnimationLayer.layer;
    layer.frame = frame;
    layer.radius = radius;
    [layer setupLayers];
    [view.layer addSublayer:layer];
    return layer;
}


- (void)setupLayers{
    //CAGradientLayer
    
    CGFloat duration = 2;
    CGFloat durationOffset = 0.3;
    CGFloat lineWidth = 3.0;
    
    CGRect rect = self.bounds;

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:_radius];
    _layers = [[NSMutableArray alloc] init];
    _animations = [[NSMutableArray alloc] init];
    NSArray *colors = @[(__bridge id)ColorWithString(@"#ff5050").CGColor, (__bridge id)ColorWithString(@"#f252ed").CGColor, (__bridge id)ColorWithString(@"#f6498a").CGColor, (__bridge id)ColorWithString(@"#ffda47").CGColor, (__bridge id)ColorWithString(@"#24cdec").CGColor];
    for (int i=0; i<2; i++) {
        CALayer *gradientLayer = [CALayer layer];
        gradientLayer.frame = rect;
        
        CAShapeLayer *layer = [CAShapeLayer layer];
        [layer setStrokeColor:[UIColor greenColor].CGColor];
        [layer setLineWidth:lineWidth];
        layer.lineCap = @"round";
        [layer setFillColor:[UIColor clearColor].CGColor];
                layer.path = path.CGPath;

        CABasicAnimation *endAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        endAnimation.fromValue = [NSNumber numberWithFloat:0.0];
        endAnimation.toValue = [NSNumber numberWithFloat:1.0];
        endAnimation.duration = duration;
        
        CABasicAnimation *startAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
        startAnimation.fromValue = [NSNumber numberWithFloat:0.0];
        startAnimation.toValue = [NSNumber numberWithFloat:1];
        startAnimation.duration = duration;
        startAnimation.beginTime = durationOffset;
        
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.animations = @[endAnimation, startAnimation];
        group.duration = duration + durationOffset;
        group.repeatCount = NSUIntegerMax;
        [layer addAnimation:group forKey:@"myStroke"];
        
        
        CAGradientLayer *color = [CAGradientLayer layer];
        color.frame = CGRectInset(gradientLayer.bounds, -lineWidth/2, -lineWidth/2);
        color.startPoint = CGPointMake(0, 0);
        color.endPoint = CGPointMake(1, 1);
        color.colors = colors;
        [gradientLayer addSublayer:color];
        
        gradientLayer.mask = layer;
        
        [self addSublayer:gradientLayer];
        
        gradientLayer.transformRotationZ = M_PI*i;
        color.transformRotationZ = -M_PI*i;
        [_layers addObject:layer];
        [_animations addObject:group];
    }
}


- (void)showAnimation{
    for (int i=0; i<_layers.count; i++) {
        CAAnimation *animation = _animations[i];
        CALayer *layer = _layers[i];
        [layer addAnimation:animation forKey:@"myStroke"];
    }
}

@end
